namespace Warehouse

open System.Windows
open System.Windows.Data
open System.Windows.Controls
open FSharp.Windows
open Warehouse.Controls

type OfferEvents = 
    | SelectedClientChanged
    | SelectedBrandChanged
    | New
    | Save
    | Print
    | IncludeAllProductsToggle of bool
    | RegularSizeCommissionForBrandChanged 
    | LitrSizeCommissionForBrandChanged 

type OfferView(control) as this = 
    inherit PartialView<OfferEvents, OfferModel, OffersControl>(control)
    
    let offes = this.Control.Offers
    let brands = this.Control.Brands
    let products = this.Control.Products
    let client = this.Control.Client
    let includeAllProductsColumn = this.Control.IncludeAllProductsColumn

    do
        this.Control.Client.DisplayMemberPath <- "CompanyName"
        offes.SynchronizedWithCurrentItem()
        this.Control.Brands.SynchronizedWithCurrentItem()

    override this.EventStreams = 
        let buttonClicks = 
            [
                this.Control.New, New
                this.Control.Save, Save
                this.Control.Print, Print
            ]
            |> List.map Button.ClickToValue

        let checkBoxColumnToggle = Event<bool>()
        let style = Style(targetType = typeof<CheckBox>, basedOn = DataGridCheckBoxColumn.DefaultEditingElementStyle)
        style.Setters.Add(
            EventSetter(
                routedEvent = CheckBox.CheckedEvent, 
                handler = RoutedEventHandler(fun _ _ -> checkBoxColumnToggle.Trigger true)
            )
        )
        style.Setters.Add(
            EventSetter(
                routedEvent = CheckBox.UncheckedEvent, 
                handler = RoutedEventHandler(fun _ _ -> checkBoxColumnToggle.Trigger false)
            )
        )
        includeAllProductsColumn.EditingElementStyle <- style

        [
            yield! buttonClicks
            
            yield client.SelectionChanged |> Observable.mapTo SelectedClientChanged
            yield brands.SelectionChangedAndNotNull() |> Observable.mapTo SelectedBrandChanged
            yield checkBoxColumnToggle.Publish |> Observable.map IncludeAllProductsToggle
            yield brands.CellEditEnding |> Observable.choose(fun eventArgs ->
                match eventArgs.EditAction, string eventArgs.Column.Header with
                | DataGridEditAction.Commit, "Regular %" -> Some RegularSizeCommissionForBrandChanged 
                | DataGridEditAction.Commit, "Litr size %" -> Some LitrSizeCommissionForBrandChanged 
                | _ -> None
            )        
        ]

    override this.SetBindings model = 
        Binding.FromExpression 
            <@
                client.ItemsSource <- model.ClientLookUp
                client.SelectedItem <- model.SelectedClient

                offes.ItemsSource <- model.Items
                offes.SelectedItem <- model.SelectedOffer

                brands.ItemsSource <- model.SelectedOffer.ByBrand
                brands.SelectedItem <- model.SelectedOffer.SelectedBrand
                products.ItemsSource <- model.SelectedOffer.SelectedBrand.Products

                products.IsEnabled <- model.SelectedOffer.SelectedBrand.IncludeAllProducts

                this.Control.Save.IsEnabled <- model.SelectedOffer.IsNewEntity
                this.Control.Print.IsEnabled <- not model.SelectedOffer.IsNewEntity
            @>        
                    

